﻿namespace NoteGateWay
{
    using System;
    using System.Configuration;
    using System.Reflection;
    using System.Windows.Forms;

    public class NoteGateWayFactory
    {
        public static NoteGateWay Create()
        {
            return Create("PathName", "ClassName");
        }

        public static NoteGateWay Create(string strKeyPathName, string strKeyClassName)
        {
            //string str = ConfigurationManager.AppSettings[strKeyClassName].ToString();
            //string str2 = ConfigurationManager.AppSettings[strKeyPathName].ToString();
            //string typeName = str2 + "." + str;
            //return (NoteGateWay) Assembly.LoadFile(Application.StartupPath + @"\" + str2 + ".dll").CreateInstance(typeName);
            return (NoteGateWay)CreateObject(ConfigurationManager.AppSettings[strKeyClassName].ToString(), ConfigurationManager.AppSettings[strKeyPathName].ToString());
        }

        public static NoteGateWay CreateViaName(string strPathName, string strClassName)
        {
            string typeName = strPathName + "." + strClassName;
            return (NoteGateWay) Assembly.LoadFile(Application.StartupPath + @"\" + strPathName + ".dll").CreateInstance(typeName);
        }

        // <summary>
        /// 创建对象或从缓存获取
        /// </summary>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            try
            {
                object objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                return objType;
            }
            catch
            {
                return null;
            }
        }
    }
}

